Skip to content

Fix SpikeDetection.load_state_dict referencing nonexistent self.running#21752

Open
Kymi808 wants to merge 1 commit into
Lightning-AI:masterfrom
Kymi808:fix/spike-detection-load-state-dict-attribute
Open

Fix SpikeDetection.load_state_dict referencing nonexistent self.running#21752
Kymi808 wants to merge 1 commit into
Lightning-AI:masterfrom
Kymi808:fix/spike-detection-load-state-dict-attribute

Conversation

@Kymi808
Copy link
Copy Markdown

@Kymi808 Kymi808 commented Jun 2, 2026

Summary

SpikeDetection in src/lightning/fabric/utilities/spike.py stores its running-mean metric on self.running_mean (set in __init__ at line 57) and saves it via state_dict under the key "running":

# line 156
"running": self.running_mean.state_dict(),

But load_state_dict then tries to restore it via self.running, an attribute that is never defined anywhere:

# line 168 (before this PR)
self.running.load_state_dict(state_dict.pop("running"))

So any resume of a SpikeDetection callback's state crashes:

src = SpikeDetection()
dst = SpikeDetection()
dst.load_state_dict(src.state_dict())
# AttributeError: 'SpikeDetection' object has no attribute 'running'

The asymmetry with the very next line — which correctly uses self.running_mean.base_metric — confirms it's a typo for self.running_mean, not a deliberate different attribute. state_dict and load_state_dict were never round-trippable.

Fix

-        self.running.load_state_dict(state_dict.pop("running"))
+        self.running_mean.load_state_dict(state_dict.pop("running"))

Test

Adds test_spike_detection_state_dict_roundtrip next to the existing tests in tests/tests_fabric/utilities/test_spike.py. It constructs two SpikeDetection instances and round-trips state_dict / load_state_dict. Fails on master with the AttributeError above; passes with this change. Reuses the same _TORCHMETRICS_GREATER_EQUAL_1_0_0 skip the surrounding tests use.

ruff check and ruff format --check clean on both files.

Notes

No related open PRs: gh pr list --repo Lightning-AI/pytorch-lightning --state all --search "SpikeDetection" returns #21664 (an Override-decorator typing change, unrelated) and #19282 (a kwarg-typo fix merged in 2024, unrelated). No test pins the current broken behavior.

`SpikeDetection.__init__` stores the running-mean metric on
`self.running_mean` (line 57). `SpikeDetection.state_dict` saves it under
the key `"running"` via `self.running_mean.state_dict()` (line 156).
`SpikeDetection.load_state_dict` then tries to restore it with
`self.running.load_state_dict(...)` — but `self.running` is never defined,
so any resume of a SpikeDetection callback state crashes with:

    AttributeError: 'SpikeDetection' object has no attribute 'running'

The asymmetry with the symmetric line right below it
(`self.running_mean.base_metric.load_state_dict(...)`) confirms it's a
typo of `self.running_mean`, not a deliberate different attribute. Adds a
regression test that round-trips state_dict / load_state_dict on a fresh
SpikeDetection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant